package com.masterclient.app.businessmodule.commonmodule;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.BaseActivity;
import com.eoner.common.config.Config;
import com.masterclient.app.R;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselib.utils.activity.UtilActivity;
import com.masterclient.app.widget.TitlebarView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AroutePath.PERSONAL_UPDATE_ENVIRONMENT)
public class UpdateEnvironmentActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitlebarView title_view;
    @BindView(R.id.publish_layout)
    View publish_layout;
    @BindView(R.id.release_layout)
    View release_layout;
    @BindView(R.id.dev_layout)
    View dev_layout;
    @BindView(R.id.et_api_url)
    EditText etApiUrl;
    @BindView(R.id.et_sapi_url)
    EditText etSApiUrl;
    @BindView(R.id.et_rapi_url)
    EditText etRApiUrl;
    @BindView(R.id.et_user_name)
    EditText etUserName;

    @BindView(R.id.et_uid)
    EditText et_uid;
    @BindView(R.id.et_invite_code)
    EditText et_invite_code;
    @BindView(R.id.et_h5_url)
    EditText et_h5_url;
    @BindView(R.id.et_detail_id)
    EditText et_detail_id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_environment);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        etApiUrl.setText("https://apida.3dintime.com");
        etSApiUrl.setText("https://apida.3dintime.com");
        etRApiUrl.setText("https://apida.3dintime.com");

        etUserName.setText("gzg_test_user=");

        String base_url = SPUtils.getInstance().getString(NetEnvManager.BASE_URL_KEY);
        if(!TextUtils.isEmpty(base_url)){
            etApiUrl.setText(base_url);
        }
        String base_surl = SPUtils.getInstance().getString(NetEnvManager.BASE_SURL_KEY);
        if(!TextUtils.isEmpty(base_surl)){
            etSApiUrl.setText(base_surl);
        }
        String base_rurl = SPUtils.getInstance().getString(NetEnvManager.BASE_RURL_KEY);
        if(!TextUtils.isEmpty(base_rurl)){
            etRApiUrl.setText(base_rurl);
        }
        String gzg_test_user = SPUtils.getInstance().getString(Config.GZG_TEST_USER_KEY);
        if(!TextUtils.isEmpty(gzg_test_user)){
            etUserName.setText(gzg_test_user.substring(1));
        }
        title_view.setRightText("查看用户缓存");

        title_view.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                if (UserDataManager.getInstance().isSystemLogin()) {
                    startActivitry(UserCheckCacheTestActivity.class, null);
                } else {
                    ToastUtil.showToast("请先登录");
                }

            }
        });
        add_listener();

    }

    private void add_listener() {

    }

    @OnClick({R.id.publish_layout, R.id.release_layout, R.id.dev_layout, R.id.tv_submit,
            R.id.debug_layout,R.id.tv_submit_uid,R.id.tv_submit_h5,R.id.tv_submit_details})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.publish_layout:
                update_environment("https://apida.3dintime.com/", "https://apida.3dintime.com/","https://apida.3dintime.com/");
                break;
            case R.id.release_layout:
                update_environment("https://apida.3dintime.com/", "https://apida.3dintime.com/","https://apida.3dintime.com/");
                break;
            case R.id.dev_layout:
                update_environment("https://apida.3dintime.com/", "https://apida.3dintime.com/","https://apida.3dintime.com/");
                break;
            case R.id.debug_layout:
                update_environment("https://apida.3dintime.com/", "https://apida.3dintime.com/","https://apida.3dintime.com/");
                break;
            case R.id.tv_submit:
                updateCustomEnvironment();
                break;
            case R.id.tv_submit_uid:
                Config.uid = TextUtils.isEmpty(et_uid.getText().toString().trim()) ? "" :et_uid.getText().toString().trim() ;
                UserDataManager.getInstance().updateInviteCode(TextUtils.isEmpty(et_invite_code.getText().toString().trim()) ? "" :et_invite_code.getText().toString().trim());
                finish();
            case R.id.tv_submit_h5:
                CommonUtil.goAnyWhere(mContext,  et_h5_url.getText().toString().trim(), "", "", "", "");
                break;
            case R.id.tv_submit_details:
                CommonUtil.goAnyWhere(mContext,  "product", et_detail_id.getText().toString().trim(), "", "", "");
                break;
        }
    }

    private void updateCustomEnvironment(){
        String apiUrl = etApiUrl.getText().toString().trim();
        String sapiUrl = etSApiUrl.getText().toString().trim();
        String rapiUrl = etRApiUrl.getText().toString().trim();
        if (TextUtils.isEmpty(apiUrl) || TextUtils.isEmpty(sapiUrl)|| TextUtils.isEmpty(rapiUrl)){
            showToast("请配置相关环境");
            return;
        }
        //TODO:退出IM
        NetEnvManager.BASE_URL = etApiUrl.getText().toString().trim();
        NetEnvManager.BASE_SURL = etSApiUrl.getText().toString().trim();
        NetEnvManager.BASE_R_URL = etRApiUrl.getText().toString().trim();
        Config.GZG_TEST_USER = ";"+etUserName.getText().toString().trim();
        SPUtils.getInstance().put(NetEnvManager.BASE_URL_KEY, NetEnvManager.BASE_URL);
        SPUtils.getInstance().put(NetEnvManager.BASE_SURL_KEY, NetEnvManager.BASE_SURL);
        SPUtils.getInstance().put(NetEnvManager.BASE_RURL_KEY, NetEnvManager.BASE_R_URL);
        SPUtils.getInstance().put(Config.GZG_TEST_USER_KEY, Config.GZG_TEST_USER);
        //TODO:清除用户信息
        UserDataManager.getInstance().clearLoginInfo();
        showToast("环境切换成功,稍后自动关闭应用");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                UtilActivity.finishAll();
                System.exit(0);
            }
        },1500);
    }

    private void update_environment(String api, String sapi, String rapi) {
        NetEnvManager.BASE_URL = api;
        NetEnvManager.BASE_SURL = sapi;
        NetEnvManager.BASE_R_URL = rapi;
        Config.GZG_TEST_USER = "";
        SPUtils.getInstance().put(NetEnvManager.BASE_URL_KEY, api);
        SPUtils.getInstance().put(NetEnvManager.BASE_SURL_KEY, sapi);
        SPUtils.getInstance().put(NetEnvManager.BASE_RURL_KEY, rapi);
        SPUtils.getInstance().put(Config.GZG_TEST_USER_KEY, Config.GZG_TEST_USER);
        //TODO:清除用户信息
        UserDataManager.getInstance().clearLoginInfo();
        showToast("环境切换成功,稍后自动关闭应用");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                UtilActivity.finishAll();
                System.exit(0);
            }
        },1500);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
